/**
 * Copyright 2014  XCL-Charts
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @Project XCL-Charts
 * @Description Android图表基类库
 * @author XiongChuanLiang<br></br>(xcl_168@aliyun.com)
 * @license http://www.apache.org/licenses/  Apache v2 License
 * @version 1.5
 */
package com.demo.xclcharts.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import org.xclcharts.chart.ArcLineChart
import org.xclcharts.chart.ArcLineData
import org.xclcharts.renderer.XEnum
import java.util.LinkedList

/**
 * @ClassName ArcLineChart01View
 * @Description  弧线比较图的例子
 * @author XiongChuanLiang<br></br>(xcl_168@aliyun.com)
 */
class ArcLineChart01View : DemoView {
    private val TAG = "ArcLineChart01View"
    private val chart = ArcLineChart()
    private val chartData = LinkedList<ArcLineData>()

    constructor(context: Context?) : super(context) {
        // TODO Auto-generated constructor stub
        initView()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initView()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        initView()
    }

    private fun initView() {
        chartDataSet()
        chartRender()
        //綁定手势滑动事件
        this.bindTouch(this, chart)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //图所占范围大小
        chart.setChartRange(w.toFloat(), h.toFloat())
    }

    private fun chartRender() {
        try {

            //设置绘图区默认缩进px值
            val ltrb = pieDefaultSpadding
            chart.setPadding(
                ltrb[0].toFloat(),
                ltrb[1].toFloat(),
                ltrb[2].toFloat(),
                ltrb[3].toFloat()
            )
            //背景
            chart.setApplyBackgroundColor(true)
            chart.setBackgroundColor(Color.WHITE)
            //边框
            chart.border.linePaint.color = Color.rgb(83, 178, 50)
            chart.showRoundBorder()

            //标题
            chart.setTitle("弧线比较图")
            chart.addSubtitle("(XCL-Charts Demo)")
            chart.setTitleVerticalAlign(XEnum.VerticalAlign.BOTTOM)

            //显示图例
            chart.plotLegend.show()

            //绑定数据源
            chart.dataSource = chartData

            //标签偏移
            chart.setLabelOffsetX(30f)
            //内环半径所占比例
            //chart.setInnerRadius(0.6f);		

            //设置附加信息
            addAttrInfo()
        } catch (e: Exception) {
            // TODO Auto-generated catch block
            Log.e(TAG, e.toString())
        }
    }

    private fun chartDataSet() {

        //设置图表数据源			
        chartData.add(ArcLineData("closed", "29% - closed", 0.29 * 100, Color.rgb(155, 187, 90)))
        chartData.add(ArcLineData("inspect", "53% - inspect", 0.53 * 100, Color.rgb(191, 79, 75)))
        chartData.add(ArcLineData("open", "76%", 0.76 * 100, Color.rgb(242, 167, 69)))
        chartData.add(ArcLineData("workdone", "86%", 0.86 * 100, Color.rgb(60, 173, 213)))
        chartData.add(ArcLineData("dispute", "36%", 0.36 * 100, Color.rgb(90, 79, 88)))
    }

    private fun addAttrInfo() {
        /////////////////////////////////////////////////////////////
        //设置附加信息
        val paintLib = Paint()
        paintLib.color = Color.rgb(46, 164, 212)
        paintLib.textAlign = Paint.Align.CENTER
        paintLib.textSize = 30f
        paintLib.isAntiAlias = true
        chart.plotAttrInfo.addAttributeInfo(XEnum.Location.TOP, "圆弧式条形图", 0.1f, paintLib)
        chart.plotAttrInfo.addAttributeInfo(XEnum.Location.BOTTOM, "XCL-Charts", 0.4f, paintLib)
        val paintSrc = Paint()
        paintSrc.textAlign = Paint.Align.CENTER
        paintSrc.textSize = 25f
        paintSrc.isAntiAlias = true
        paintSrc.color = Color.rgb(41, 34, 102)
        chart.plotAttrInfo.addAttributeInfo(
            XEnum.Location.BOTTOM,
            "ExcelPro", 0.2f, paintSrc
        )
        /////////////////////////////////////////////////////////////
    }

    override fun render(canvas: Canvas) {
        try {
            chart.render(canvas)
        } catch (e: Exception) {
            Log.e(TAG, e.toString())
        }
    }
}